const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

//随机生成32位字符串
const randomString = () => {
  //默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
  let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  let maxPos = chars.length
  let character = ''
  for (let i = 0; i < 32; i++) {
    character += chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return character
}
//时间戳随机生成32位字符串 唯一 订单号 最好由数字组成
const randomTradeNo = () => {
  const timestamp = new Date().getTime()
  const timeLen = timestamp.toString().length
  const len = 32 - timeLen
　　let chars = '0123456789'  
　　let maxPos = chars.length
　　let randomStr = ''
　　for (let i = 0; i < len; i++) {
     randomStr += chars.charAt(Math.floor(Math.random() * maxPos))
　　}
　　return randomStr + timestamp
}

//时间戳转换日期格式
const formatSjc = (sjc, type)=> {
  if (/^\d+$/.test(sjc)) {
    sjc = sjc * 1000
  }
  var time = new Date(sjc);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  if (type == 1) {
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm)
  } else if (type == 2) {
    return add0(m) + '-' + add0(d)
  } else if (type == 3) {
    return y + '-' + add0(m) + '-' + add0(d)
  } else if (type == 4) {
    return add0(h) + ':' + add0(mm) + ':' + add0(s)
  }
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

function add0(t) {
  t = t > 9 ? t : '0' + t
  return t
}

//获取当前

//获取日期 
const getDate = (date, AddDayCount = 0)=> {
  if (!date) {
    date = new Date()
  }
  if (typeof date !== 'object') {
    date = date.replace(/-/g, '/')
  }
  const dd = new Date(date)

  dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

  const y = dd.getFullYear()
  const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
  const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
  return {
    fullDate: y + '-' + m + '-' + d,
    year: y,
    month: m,
    date: d,
    day: dd.getDay()
  }
}

//比较相差几天 格式'2021-11-02'
const dateDiffIncludeToday = (startDateString, endDateString) => {
  var separator = "-"; //日期分隔符
  var startDates = startDateString.split(separator);
  var endDates = endDateString.split(separator);
  var startDate = new Date(startDates[0], startDates[1] - 1, startDates[2]);
  var endDate = new Date(endDates[0], endDates[1] - 1, endDates[2]);
  return parseInt(Math.abs(endDate - startDate) / 1000 / 60 / 60 / 24) + 1; //把相差的毫秒数转换为天数
}

//下拉刷新
const getMore = (length, count, limit, backfun) => {
  if (length == count) {
    return true
  } else {
    let num = Math.ceil(length / limit) + 1
    backfun(num)
  }
}

//服务器时间转化 2021-12-08T06:28:29.303Z
const rTime = (UTCDateString)=>{
  if (!UTCDateString) {
    return '-';
  }
  function formatFunc(str) {
    return str > 9 ? str : '0' + str
  }
  var date2 = new Date(UTCDateString);
  console.log('时间', date2)
  var year = date2.getFullYear();
  var mon = formatFunc(date2.getMonth() + 1);
  var day = formatFunc(date2.getDate());
  var hour = date2.getHours();
  var noon = hour >= 12 ? 'PM' : 'AM'; // 判断是上午还是下午
  //hour = hour >= 12 ? hour - 12 : hour; // 12小时制
  hour = formatFunc(hour);
  var min = formatFunc(date2.getMinutes());
  var sec = formatFunc(date2.getSeconds());
  var dateStr = year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + sec;
  return dateStr;
}

// 比较时间大小
const compareDate = (date1,date2)=>{
  var oDate1 = new Date(date1);
  var oDate2 = new Date(date2);
  if(oDate1.getTime() > oDate2.getTime()){
      return true; //第一个大
  } else {
      return false; //第二个大
  }
}

//和当前时间比较
const compareDateNow = (date)=>{
  var oDate1 = new Date(date)
  var oDate2 = new Date()
  if(oDate1.getTime() > oDate2.getTime()){
      return false //大于当前时间 
  } else {
      return true //小于当前时间 过期
  }
}

//倒计时 和当前时间 毫秒差
const countDownNum = (date)=>{
  var oDate1 = new Date(date)
  var oDate2 = new Date()
  return oDate1.getTime() - oDate2.getTime()
}


module.exports = {
  formatTime,
  randomString,
  randomTradeNo,
  formatSjc,
  getDate,
  dateDiffIncludeToday,
  getMore,
  rTime,
  compareDateNow,
  countDownNum 
}
